/**
 * 初始化收银中心详情对话框
 */
var TOrderFeeInfoDlg = {
    tOrderFeeInfoData: {},
    validateFields: {
        fee_id: {
            validators: {
                notEmpty: {
                    message: '费用类型不能为空'
                }
            }
        },
        money: {
            validators: {
                notEmpty: {
                    message: '金额不能为空'
                }
            }
        },
        applicant: {
            validators: {
                notEmpty: {
                    message: '申请人不能为空'
                }
            }
        }
    }
};
TOrderFeeInfoDlg.validate = function () {
    $('#orderFeeFrom').data("bootstrapValidator").resetForm();
    $('#orderFeeFrom').bootstrapValidator('validate');
    return $("#orderFeeFrom").data('bootstrapValidator').isValid();
};
/**
 * 清除数据
 */
TOrderFeeInfoDlg.clearData = function () {
    this.tOrderFeeInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
TOrderFeeInfoDlg.set = function (key, val) {
    this.tOrderFeeInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
TOrderFeeInfoDlg.get = function (key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
TOrderFeeInfoDlg.close = function () {
    parent.layer.close(window.parent.TOrderFee.layerIndex);
}

/**
 * 收集数据
 */
TOrderFeeInfoDlg.collectData = function () {
    var fee_id=$('#fee_id').val().split("_")[0];
    this.set("feeId",fee_id)
        .set('money')
        .set('auditStatus')
        .set('applicant')
        .set('remark')
        .set('orderId',$('#order_id').val())
    ;
}

/**
 * 提交添加
 */
TOrderFeeInfoDlg.addSubmit = function () {

    this.clearData();
    this.collectData();
    var fee_id=$('#fee_id').val().split("_")[0];
    if($('#fee_id').val().split("_")[1]=='1'){

        if($("#order_id").val()==''){
            Feng.error("请关联单号!");
            return;
        }

    }
    if (!this.validate()) {
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/tOrderFee/add", function (data) {
        Feng.success("添加成功!");
        window.parent.aTable.refresh();
        TOrderFeeInfoDlg.close();
    }, function (data) {
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.tOrderFeeInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
TOrderFeeInfoDlg.editSubmit = function () {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/tOrderFee/update", function (data) {
        Feng.success("修改成功!");
        window.parent.aTable.refresh();
        TOrderFeeInfoDlg.close();
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.tOrderFeeInfoData);
    ajax.start();
}

$(function () {

    Feng.initValidator("orderFeeFrom", TOrderFeeInfoDlg.validateFields);

    $("#orderNum").bsSuggest('init', {
        /*url: "/rest/sys/getuserlist?keyword=",
        searchFields: [ "shortAccount"],
        effectiveFieldsAlias:{userName: "姓名"},*/
        effectiveFields: ["id", "original_num"],
        clearable: true,
        allowNoKeyword: false,
        url: Feng.ctxPath + "/tOrder/ordersNum?query=",
        getDataMethod: "url",
        idField: "id",
        delayUntilKeyup: true,
        keyField: "original_num",
        fnPreprocessKeyword: function (keyword) {
            return encodeURI(keyword, "UTF-8");
        },
        processData: function (result) {
            var data = { value: [] };
            // for(var i=0;result.length<i;i++)
            $.each(result, function (i, r) {
                data.value.push({ original_num: r.original_num,id:r.id })
            })
            return data;
        }
    }).on('onDataRequestSuccess', function (e, result) {
        // console.log('onDataRequestSuccess: ', result);
    }).on('onSetSelectValue', function (e, keyword, data) {
        console.log('onSetSelectValue: ', keyword, data);
        // alert(keyword.id)
        $("#order_id").val(keyword.id);
    }).on('onUnsetSelectValue', function () {
        // console.log('onUnsetSelectValue');
        $("#order_id").val('');
    });
});
